package net.yxsoft.controller.pc.sysmgt.common;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Record;
import net.yxsoft.controller.pc.sysmgt.common.interceptor.AuthInterceptor;
import net.yxsoft.controller.pc.sysmgt.common.interceptor.LoginDbSessionInterceptor;
import net.yxsoft.service.infrastructure.UserService;
import rapid.core.Authuser;
import rapid.core.ContextStateEnum;
import rapid.core.TResult;
import rapid.jfinal.BaseController;

import java.util.ArrayList;
import java.util.List;

/**
* @MethodName: 系统管理权限认证控制器
* @Description:
* @param: null
* @return * @return null
* @throws
* @date 2023/8/31 15:32
* @author jhf
*/
@Before({LoginDbSessionInterceptor.class, AuthInterceptor.class})
public class AuthController extends BaseController {
  public AuthController() {
    result = new TResult();
  }
  /**
  * @MethodName: getUserGroups
  * @Description:
  * @param:
  * @return java.util.List<com.jfinal.plugin.activerecord.Record>
  * @throws
  * @date 2023/8/31 15:32
  * @author jhf
  */
  protected List<Record> getUserGroups() {
    Authuser u = getLoginUser();

    if(u == null) {
      return new ArrayList<Record>();
    }

    return UserService.INSTANCE.findUserGroups(u.getId());
  }
  /**
  * @MethodName: getLoginUser
  * @Description:
  * @param:
  * @return rapid.core.Authuser
  * @throws
  * @date 2023/8/31 15:32
  * @author jhf
  */
  protected Authuser getLoginUser() {


    return getSessionAttr(Authuser.KEY_USER);
  }

  /**
  * @MethodName: sessionTimeout
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:33
  * @author jhf
  */
  protected void sessionTimeout() {
    result.setState(ContextStateEnum.FAIL);
    result.setMessage("操作超时，请重新登录。");
    renderJson(result);
  }
  /**
  * @MethodName: isLogin
  * @Description:
  * @param:
  * @return boolean
  * @throws
  * @date 2023/8/31 15:33
  * @author jhf
  */
  protected boolean isLogin() {
    return getLoginUser() != null;
  }
  /**
  * @MethodName: notLogin
  * @Description:
  * @param:
  * @return boolean
  * @throws
  * @date 2023/8/31 15:33
  * @author jhf
  */
  protected boolean notLogin() {
    return !isLogin();
  }



}
